#coding=utf-8
import time
import unittest
from selenium import webdriver
from loginEgovaMobile import login_factory
from recReport import rec_report_factory
import HTMLTestRunner
import random
from Tool.usefulTools import userfulToolsFactory

desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '6.0.1'
desired_caps['deviceName'] = '10463120'
# desired_caps['platformVersion'] = '4.4.2'
# desired_caps['deviceName'] = 'S7TDU15523003665'
desired_caps['appPackage'] = 'cn.com.egova.egovamobile'
desired_caps['appActivity'] = '.login.LoginActivity'
desired_caps['unicodeKeyboard'] = True
desired_caps['resetKeyboard'] = True

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

driver.implicitly_wait(60)

server_url = "http://106.74.36.89:8080/eUrbanMIS"
user_name = 'egova'
user_pwd = 'egova.2017'

base_dir =  'D://Cruise//pythonSrc//reprot//'

loginDevices = login_factory()
recReport = rec_report_factory()
userfulTool = userfulToolsFactory()

relative_address = userfulToolsFactory.creat_dir(userfulTool,base_dir)
pic_file_path = base_dir + '//' + relative_address

class StartTest(unittest.TestCase):

    def set_server_url(self):
        print "test"
        login_factory.modify_server_address(loginDevices,driver,server_url,pic_file_path)

    def login_egova(self):
        # print 'login'
        login_factory.login_system(loginDevices,driver,user_name,user_pwd,pic_file_path)

    def rec_report(self):
        rec_report_factory.rec_report_excute(recReport,driver)


if __name__ == '__main__':
    suite = unittest.TestSuite()
    # 添加测试用例
    suite.addTest(StartTest('set_server_url'))
    suite.addTest(StartTest('login_egova'))

    # 添加报告名称和地址
    #timestr = time.strftime('%Y-%m-%d', time.localtime(time.time()))  # 本地日期时间作为测试报告的名字
    #print timestr
    #filename = 'D://Cruise//pythonSrc//reprot//egova-mobile-' + timestr + '-' + str(random.uniform(5,500)) + '.html'  # 这个路径改成自己的目录路径
    filename = base_dir + 'egova-test-' + relative_address + '.html'
    print filename
    fp = file(filename, 'wb')
    runner = HTMLTestRunner.HTMLTestRunner(
        stream=fp,
        title='result',
        description='report'
    )
    runner.run(suite)
    fp.close()
    unittest.main()
